/******************************init****************************/
function queryCommodity(page) {
	var parm = $('#queryCommodityForm').serialize() + (page != null ? ('&page=' + page) : '');
	var url = create_url('commodity-query');
	sys_record_parm('commodity',parm);
	sys_loading();
	$.ajax({type:'POST', url:url, data:parm,
		success: function(result) {
			sys_loading_close();
			$('#resultList').html(result);
		}
	});
}
function deleteCommodity(code) {
	$.Zebra_Dialog('是否删除',{
	'type': 'question',
	'title': '警告' ,
	'buttons':[
       {caption:'删除',callback:function() {
    	 	var url = create_url('commodity-delete');
			commodity_aidExecute(code, url);
		}},
       {caption:'取消',callback:function(){}
       }]
	});
}

function commodity_aidExecute(code, url) {
	var parm = {'code':code};
	sys_submiting();
	$.ajax({type:'POST', url:url, data:parm,
		success: function(result) {
			sys_submiting_close();
			if ('success' == result) {
				sys_msg('操作成功');
				queryCommodity();
			} else {
				sys_msg('操作失败');
				queryCommodity();
			}
		}
	});
}
function applyIndexShow(btn, code, oc) {
	$.Zebra_Dialog('是否要申请展示',{
		'type': 'question',
		'title': '警告' ,
		'buttons':[
           {caption:'申请',callback:function() {
    		var parm = {'code':code, 'oc':oc };
			var url = create_url('parkcommodity-applyIndexShow');
			sys_submiting();
			$.ajax({type:'POST', url:url, data:parm,
				success: function(result) {
					sys_submiting_close();
					if ('success' == result) {
						sys_msg('申请成功');
						queryCommodity();
					} else {
						sys_msg('操作失败');
						queryCommodity();
					}
				}
			});
		}},
       {caption:'取消',callback:function(){}
    	}]
		});
}


/*************edit*****************/
var content = null;

function saveCommodity() {
	setCover();
	var parm = $('#editCommodityForm').serialize() + '&content=' + fixJqText(Url.encode(content.getData()));
	var url = create_url('commodity-save');
	sys_submiting();
	$.ajax({type:'POST', url:url, data:parm,
		success: function(result) {
		sys_submiting_close();
			if ('success' == result) {
				sys_msg('操作成功!');
				sys_load_handle('commodity');
			}
		}
	});
}
function selectCategory() {
		var url = create_url('commodity-adiCategoryInit');
		var parm = {};
		$.colorbox({overlayClose:false, href:url, data:parm, close:'[关闭]', width:'400px', height:'600px', open:true});
}

function addFilesUploadEvent_Commodity() {
	var url = create_url('commodity-uploads');
	var btn = $('#uploadFilesBtn');
	new AjaxUpload(btn, {
		action:url,
		name:'files',
		multiple: true,
		responseType: 'json',
		onSubmit : function(file, ext) {
			if (ext && /^(jpg|png|jpeg|gif|bmp|JPG|PNG|JPEG|GIF|BMP)$/.test(ext)){
				this.setData({'info':'文件类型正确'});
			} else {
				sys_msg('文件类型错误', '只能上传类型为：jpg、png、jpeg、gif、bmp、图片文件!尺寸小于300*300最佳');
				return false;               
			}
			$('#upload_span').html(sys_loading_img);
			this.disable();
		},
		onComplete: function(files, response){
			if ('error' == response.result)
				sys_msg('失败', '上传文件失败，请检查文件名是否正确!');
			else if ('file_type_error' == response.result)
				sys_msg('文件类型错误', '只能上传类型为：jpg、png、jpeg、gif、bmp、图片文件!');
			else {
				var template = $('#tempAnnexListDiv').find('.rep');
				var annexList = $('#annexListTable');
				var index = parseInt($('#commodity_an_index').val());
				$.each(response.list, function(i) {
					var pii = template.clone(true);
					pii.find('.title').html(response.list[i].name).end()
						.find('.name').attr('name', 'List[' + (index) + '].name').val(response.list[i].name).end()
						.find('.size').attr('name', 'List[' + (index) + '].size').val(response.list[i].size).end()
						.find('.sn').attr('name', 'List[' + (index) + '].sn').val(response.list[i].path).end()
						.find('.ext').attr('name', 'List[' + (index) + '].ext').val(response.list[i].ext).end()
						.find('.afcs').val(response.list[i].code).end()
						.find('.font-cover').val(response.list[i].code).end();
					annexList.append(pii);
					index ++;
					$('#commodity_an_index').val(index);
				});
				this.enable();
			}
		}
	});
}
function deleteSelectedItem(btn) {
	btn.parents('.rep').remove();
}
function setCover(){
	var cover = $("input[name=choose]:checked").val();
	$("#fileCode").val(cover);
}
function checkedCommodity(){
	var name = $("#name").val();
	var price = $("#price").val();
	var categoryName = $("#categoryName").val();
	if(categoryName.length==0){
		sys_msg('必须选择商品类型');
		return
	}
	if(name.length==0){
		sys_msg('必须填写商品名称');
		return
	}
	/*if(price.length==0){
		sys_msg('必须商品价格');
		return
	}*/
	saveCommodity();
}
/************aidinit******************/

function queryCategory(page) {
	var parm = $('#queryCategoryForm').serialize() + (page != null ? ('&page=' + page) : '');
	var url = create_url('commodity-adiCategoryList');
	sys_loading();
	$.ajax({type:'POST', url:url, data:parm,
		success: function(result) {
			sys_loading_close();
			$('#resultList').html(result);
		}
	});
}
function chooseCategory(code, name) {
	$("#cc").val(code);
	$("#categoryName").val(name);
	$.fn.colorbox.close();
}

$(".txt_userlist_name").keydown(function(event) {
	if (event.keyCode == '13') {
		queryCategory();
		return false;
	}
});

